﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Xml;
using System.Xml.Serialization;

namespace Serialize
{
    public static class Serializer
    {
        #region XmlSerializer序列化和反序列化
        /// <summary>
        ///  XML序列化
        /// </summary>
        /// <typeparam name="T">将要序列化的类型</typeparam>
        /// <param name="filePath">文件路径</param>
        /// <param name="instance">将要序列化的类型实例</param>
        public static void XmlSerialize<T>(string filePath, T instance)
        {
            using (FileStream fs = File.Create(filePath))
            {
                using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    try
                    {
                        serializer.Serialize(writer, instance);
                    }
                    catch
                    {
                        throw;
                    }
                }
            }
        }
        /// <summary>
        /// XML反序列化
        /// </summary>
        /// <typeparam name="T">将要反序列化的类型</typeparam>
        /// <param name="filePath">文件路径</param>
        /// <returns>反序列化后的类型实例 </returns>
        public static T XmlDeserialize<T>(string filePath)
        {
            using (FileStream fs = File.OpenRead(filePath))
            {
                using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas()))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    try
                    {
                        return (T)serializer.Deserialize(reader);
                    }
                    catch
                    {
                        throw;
                    }
                }
            }
        }
        #endregion

        #region DataContractSerializer序列化和反序列化
        /// <summary>
        ///  DataContract序列化
        /// </summary>
        /// <typeparam name="T">将要序列化的类型</typeparam>
        /// <param name="filePath">文件路径</param>
        /// <param name="instance">将要序列化的类型实例</param>
        public static void DataContractSerialize<T>(string filePath, T instance)
        {
            using (FileStream fs = File.Create(filePath))
            {
                using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs))
                {
                    DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                    try
                    {
                        serializer.WriteObject(writer, instance);
                    }
                    catch
                    {
                        throw;
                    }
                }
            }
        }
        /// <summary>
        /// DataContract反序列化
        /// </summary>
        /// <typeparam name="T">将要反序列化的类型</typeparam>
        /// <param name="filePath">文件路径</param>
        /// <returns>反序列化后的类型实例。</returns>
        public static T DataContractDeserialize<T>(string filePath)
        {
            using (FileStream fs = File.OpenRead(filePath))
            {
                using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas()))
                {
                    DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                    try
                    {
                        return (T)serializer.ReadObject(reader);
                    }
                    catch
                    {
                        throw;
                    }
                }
            }
        }
        #endregion

        #region BinaryFormatter序列化和反序列化
        /// <summary>
        /// 二进制序列化
        /// </summary>
        /// <typeparam name="T">将要序列化的类型</typeparam>
        /// <param name="filePath">文件路径</param>
        /// <param name="instance">将要序列化的类型实例</param>
        public static void BinarySerialize<T>(string filePath, T instance)
        {
            using (FileStream fs = File.Create(filePath))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                try
                {
                    binaryFormatter.Serialize(fs, instance);
                }
                catch
                {

                    throw;
                }
            }
        }
        /// <summary>
        /// 二进制反序列化
        /// </summary>
        /// <typeparam name="T">将要反序列化的类型</typeparam>
        /// <param name="filePath">文件路径</param>
        /// <returns>反序列化后的类型实例</returns>
        public static T BinaryDeserialize<T>(string filePath)
        {
            using (FileStream fs = File.OpenRead(filePath))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                try
                {
                    return (T)binaryFormatter.Deserialize(fs);
                }
                catch
                {

                    throw;
                }
            }
        }
        #endregion

        #region JavaScriptSerializer序列化和反序列化
        /// <summary>
        ///  将对象转换为 JSON 字符串。
        /// </summary>
        /// <typeparam name="T">要序列化的类型</typeparam>
        /// <param name="filePath">文件路径</param>
        /// <param name="instance">要序列化的类型实例</param>
        /// <param name="encoding">字符编码</param>
        public static void JavaScriptSerialize<T>(string filePath, T instance, Encoding encoding)
        {
            using (FileStream fs = File.Create(filePath))
            {
                try
                {
                    byte[] buffer = encoding.GetBytes(new JavaScriptSerializer().Serialize(instance));
                    fs.Write(buffer, 0, buffer.Length);
                }
                catch
                {

                    throw;
                }
            }
        }
        /// <summary>
        /// 将指定的 JSON 字符串转换为 T 类型的对象。
        /// </summary>
        /// <typeparam name="T">将要反序列化的类型</typeparam>
        /// <param name="filePath">文件路径</param>
        /// <returns>反序列化后的类型实例 </returns>
        public static T JavaScriptDeserialize<T>(string filePath)
        {
            try
            {
                return new JavaScriptSerializer().Deserialize<T>(File.ReadAllText(filePath));
            }
            catch
            {

                throw;
            }
        }
        #endregion
    }
}
